package com.jx.smartbot.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jx.smartbot.util.JacksonUtil;

import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@Controller
@RequestMapping("/biz/wp")  
public class WpController {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(WpController.class);
	//private String appName="wpapp";
	@Resource(name="clientUrl")
	private String clientUrl;
	public void init(){}
	//tools
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "/tools", method = RequestMethod.POST)
	public Object wp_tools(@RequestParam(value = "token", defaultValue = "") String token,
			@RequestBody  String Datajson,
			HttpServletRequest reques,HttpServletResponse response)throws IOException{
		String clientUrl_=this.str("wpclient/tools",token,Datajson);
		logger.info(clientUrl_);
		response.sendRedirect(clientUrl_);
		return null;
	}
	//groups
	@CrossOrigin(origins = "*")
	@RequestMapping(value = "groups", method = RequestMethod.POST)
	public Object wp_groups(@RequestParam(value = "token", defaultValue = "") String token,
			@RequestBody  String Datajson,
			HttpServletRequest reques,HttpServletResponse response)throws IOException{
		String clientUrl_=this.str("wpclient/groups",token,Datajson);
		logger.info(clientUrl_);
		response.sendRedirect(clientUrl_);
		return null;
	}
	
	//@SuppressWarnings("deprecation")
	private String str(String htmlfile,String token,String Datajson){
		String clientUrl_=clientUrl+htmlfile+"?token=wp_"+token;
		String urlEncoded ="";
		try {
			 //urlEncoded ="&data=" + Base64.getUrlEncoder().encodeToString(Datajson.getBytes("utf-8"));
			 urlEncoded = Base64.getEncoder().encodeToString(Datajson.getBytes("utf-8"));
			 urlEncoded=URLEncoder.encode(urlEncoded,"utf-8").replaceAll("%2B", "%2b");
			 urlEncoded ="&data=" +  urlEncoded;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clientUrl_+urlEncoded;
	}
	public String str2(String htmlfile,String token,String Datajson){
		String clientUrl_=clientUrl+htmlfile+"?token="+token;
		@SuppressWarnings("unchecked")
		Map<String,Object> map=JacksonUtil.readValue(Datajson, Map.class);
		StringBuffer sb =new StringBuffer();
		for(String key:map.keySet()){
			sb.append("&");
			sb.append(key);
			sb.append("=");
			sb.append(map.get(key));
		}
		return clientUrl_+sb.toString();
	}
}
